/*
 *  $Id: Turnskin.cs 975 2009-12-16 17:13:22Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

namespace Ragima.Affects {
    /// <summary>
    /// Эффект: превращение
    /// Содержит список форм, представляемых названием и подэффектами,
    /// между которыми может превращаться владелец эффекта.
    /// </summary>
    public class Turnskin: Affect {
        private static readonly Languages.Phrase Action = new Languages.Phrase("affect.turnskin.action");

        private List<TurnskinForm> _forms;
        private int _current;

        public Turnskin() {
            _forms = new List<TurnskinForm>();
            _current = 0;
        }

        private TurnskinForm Current {
            get { return _current >= 0 && _current < _forms.Count ? _forms[_current] : null; }
        }

        public override string Id {
            get { return "turnskin"; }
        }

        public override bool Dispellable {
            get { return false; }
        }

        public override AbstractObject Owner {
            set {
                base.Owner = value;
                if (Current != null) Current.Owner = value;
            }
        }

        public override int BaseWeight {
            get {
                int sum = 0;
                foreach (TurnskinForm form in _forms)
                    sum += form.BaseWeight;
                return sum/_forms.Count + 20;
            }
        }

        public override int GetWeight(Card card) {
            int sum = 0;
            foreach (TurnskinForm form in _forms)
                sum += form.GetWeight(card);
            return sum/_forms.Count + 20;
        }


        public override Affect Clone() {
            Turnskin copy = new Turnskin();
            foreach (TurnskinForm form in _forms) copy._forms.Add(form.Clone() as TurnskinForm);
            return copy; 
        }

        public override void SetAffect() {
            base.SetAffect();
            LocalSummary.OnDeath += DoDeath;
            if (Current != null) Current.SetAffect();
        }
        public override void ResetAffect() {
            base.ResetAffect();
            LocalSummary.OnDeath -= DoDeath;
            if (Current != null) Current.ResetAffect();
        }

        private void DoDeath(AbstractObject obj) {
            SetForm(0, false);
        }

        public int Count {
            get { return _forms.Count; }
        }

        public void AddForm(Languages.Names name, bool turned, params Affect[] subAffects) {
            TurnskinForm form = new TurnskinForm(name, turned);
            foreach (Affect affect in subAffects) form.Add(affect);
            _forms.Add(form);
        }

        public void SetForm(int index, bool logged) {
            if (index == _current) return;
            if (Current != null && Current.Owner != null) {
                Current.ResetAffect();
                Current.Owner = null;
            }
            _current = index;
            if (Current != null && Owner != null) {
                Current.Owner = Owner;
                Current.SetAffect();
                if (logged) Game.Current.Log.Add(Action, Owner, Current);
                if (Owner is Creature) ((Creature) Owner).SetTurned(Current.TurnedCard);
            }
        }

        public void NextForm() {
            int newIndex =_current + 1;
            if (newIndex >= Count) newIndex = 0;
            SetForm(newIndex, true);
        }

        private class TurnskinForm: GroupAffect, Languages.INamed {
            public readonly Languages.Names FormName;
            public readonly bool TurnedCard;

            public TurnskinForm(Languages.Names name, bool turned): base() {
                FormName = name;
                TurnedCard = turned;
            }

            Languages.Names Languages.INamed.Name {
                get { return FormName; }
            }

            public override string Id {
                get { return "turnskin.form"; }
            }

            public override Affect Clone() {
                TurnskinForm copy = new TurnskinForm(FormName, TurnedCard);
                copy.AddCloned(this);
                return copy;
            }
        }
    }
}